package com.sskj.common.widget;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;


import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.R;
import com.sskj.common.util.DisposUtil;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MyProgressDialogs {
    private Dialog dialog;
    private Context context;
    private static final int CLOSE_SAMPLE_VIEW = 0;
    private DelayCloseController delayCloseController;
    private Handler mHandler = new MyHandler(this);
    private int time;
    private Disposable timeDispo;

    public MyProgressDialogs(Context context) {
        this.context = context;
    }


    public void showDialog() {
        this.showDialog("加载中");
    }

    public void showDialog(String msg) {
        this.dialog = (new Builder(this.context)).create();
        this.dialog.show();
        this.dialog.setCancelable(false);
        Window window = this.dialog.getWindow();
        window.setContentView(R.layout.common_dialog_loading);
        window.setBackgroundDrawable(new BitmapDrawable());
//        TextView textView = (TextView) window.findViewById(R.id.loadingText);
        MyMaterialHeader myMaterialHeader = (MyMaterialHeader) window.findViewById(R.id.myMaterialHeader);

//
//        if (TextUtils.isEmpty(msg)) {
//            textView.setVisibility(View.GONE);
//        } else {
//            textView.setVisibility(View.VISIBLE);
//            textView.setText(msg);
//        }
        time = 0;
        DisposUtil.close(timeDispo);
        timeDispo = Flowable.interval(0, 20, TimeUnit.MILLISECONDS)
                .onBackpressureDrop()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .map(aLong -> time++)
                .subscribe(aLong -> {
                    myMaterialHeader.onPositionChange(aLong % 100);
                });

        this.dialog.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == 4 && event.getAction() == 0) {
                    dialog.dismiss();
                }

                return false;
            }
        });
    }

    public void closeDialog() {
        if (this.dialog != null && this.dialog.isShowing()) {
            DisposUtil.close(timeDispo);
            this.dialog.dismiss();
            this.dialog = null;
        }

    }

    public static class MyHandler extends Handler {
        private WeakReference<MyProgressDialogs> weakReference = null;

        public MyHandler(MyProgressDialogs progressDialogs) {
            this.weakReference = new WeakReference(progressDialogs);
        }

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (((MyProgressDialogs) this.weakReference.get()).dialog != null && ((MyProgressDialogs) this.weakReference.get()).dialog.isShowing()) {
                        ((MyProgressDialogs) this.weakReference.get()).closeDialog();
                    }
                default:
            }
        }
    }

    private class DelayCloseController extends TimerTask {
        private Timer timer = new Timer();
        private int actionFlags = 0;

        public DelayCloseController() {
            this.timer.schedule(this, 30000L);
        }

        public void setCloseFlags(int flag) {
            this.actionFlags = flag;
        }

        public void run() {
            Message messageFinish = new Message();
            messageFinish.what = this.actionFlags;
            mHandler.sendMessage(messageFinish);
        }
    }
}
